<?php

class SalestaxesController extends AppController {

    var $name = 'Salestaxes';
    var $uses = array('Salestax', 'State');

    function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allowedActions = array('*');
    }

    function index($id=null) {
        $this->layout = false;
        $this->Salestax->recursive = 0;
        if (!empty($id)) {
            $this->paginate = array(
                'Salestax' => array('limit' => 100,
                    'conditions' => array('OR' => array(array('Salestax.product_id' => $id, 'Salestax.seller_id' => $this->Auth->user('id')))),
                    'order' => array('State.name' => 'asc'
                )));
            $this->data['product_id'] = $id;
        } else {
            $this->paginate = array(
                'Salestax' => array('limit' => 100,
                    'conditions' => array('OR' => array(array('Salestax.seller_id' => $this->Auth->user('id')))),
                    'order' => array('State.name' => 'asc'
                )));
        }
        $this->set('salestaxes', $this->paginate());
        $states = $this->Salestax->State->find('list', array('fields' => array('State.id', 'State.name'), 'order' => array('State.name ASC')));
        
        $this->set(compact('states'));
    }

    function seller($id=null) {
        $this->layout = "innerhome";
        $this->Salestax->recursive = 0;
        if (!empty($id)) {
            $this->paginate = array(
                'Salestax' => array('limit' => 100,
                    'conditions' => array('OR' => array(array('Salestax.product_id' => $id, 'Salestax.seller_id' => $this->Auth->user('id')))),
                    'order' => array('State.name' => 'asc'
                )));
            $this->data['product_id'] = $id;
        } else {
            $this->paginate = array(
                'Salestax' => array('limit' => 100,
                    'conditions' => array('OR' => array(array('Salestax.seller_id' => $this->Auth->user('id')), array('Salestax.seller_id' => 1))),
                    'order' => array('State.name' => 'asc'
                )));
        }
        $this->set('salestaxes', $this->paginate());
        $states = $this->Salestax->State->find('list', array('fields' => array('State.id', 'State.name'), 'order' => array('State.name ASC')));

        $this->set(compact('states'));
    }

    function sellerajax($id=null) {
        $this->layout = null;
        $this->Salestax->recursive = 0;

        $this->paginate = array(
            'Salestax' => array('limit' => 100,
                'conditions' => array('OR' => array(array('Salestax.seller_id' => $this->Auth->user('id')))),
                'order' => array('State.name' => 'asc'
            )));

        $this->set('salestaxes', $this->paginate());
         $states = $this->Salestax->State->find('list', array('fields' => array('State.id', 'State.name'), 'order' => array('State.name ASC')));
        
        $this->set(compact('states'));
    }

    function add($id=null) {
        $this->layout = false;
        if (!empty($this->data)) {
            $this->data['Salestax']['seller_id'] = $this->Auth->user('id');
            $this->Salestax->create();
            if ($this->Salestax->save($this->data)) {
                $this->Session->setFlash(__('The sales tax has been saved', true));
                $this->redirect(array('action' => 'index',$id));
            } else {
                $this->Session->setFlash(__('The sales tax could not be saved. Please, try again.', true));
                //$this->redirect(array('action' => 'index'));
            }
        }
        $this->Salestax->recursive = 0;
//        $this->paginate = array(
//            'Salestax' => array('limit' => 100, 'conditions' => array('Salestax.seller_id' => $this->Auth->user('id')),
//                'order' => array('State.name' => 'asc'
//            )));
        
        if (!empty($id)) {
            $this->paginate = array(
                'Salestax' => array('limit' => 100,
                    'conditions' => array('OR' => array(array('Salestax.product_id' => $id, 'Salestax.seller_id' => $this->Auth->user('id')))),
                    'order' => array('State.name' => 'asc'
                )));
            $this->data['product_id'] = $id;
        } else {
            $this->paginate = array(
                'Salestax' => array('limit' => 100,
                    'conditions' => array('OR' => array(array('Salestax.seller_id' => $this->Auth->user('id')))),
                    'order' => array('State.name' => 'asc'
                )));
        }
        $this->set('salestaxes', $this->paginate());
        $states = $this->Salestax->State->find('list', array('fields' => array('State.id', 'State.name'), 'order' => array('State.name ASC')));
        $this->set(compact('states'));
    }

    function add_seller() {
        $this->layout = null;
        if (!empty($this->data)) {
            $this->data['Salestax']['seller_id'] = $this->Auth->user('id');
            $this->Salestax->create();
            if ($this->Salestax->save($this->data)) {
                $this->Session->setFlash(__('The sales tax has been saved', true));
                $this->redirect(array('action' => 'sellerajax'));
            } else {
                $this->Session->setFlash(__('The sales tax could not be saved. Please, try again.', true));
                //$this->redirect(array('action' => 'index'));
            }
        }
        $this->Salestax->recursive = 0;
        $this->paginate = array(
            'Salestax' => array('limit' => 100, 'conditions' => array('Salestax.seller_id' => $this->Auth->user('id')),
                'order' => array('State.name' => 'asc'
            )));
        $this->set('salestaxes', $this->paginate());
        $states = $this->Salestax->State->find('list', array('fields' => array('State.id', 'State.name'), 'order' => array('State.name ASC')));
        $this->set(compact('states'));
    }

    function backstage_index() {
        $this->layout = 'admin';
        $this->Salestax->recursive = 0;
        $this->paginate = array(
            'Salestax' => array('limit' => 100, 'conditions' => array('Salestax.seller_id' => $this->Auth->user('id')),
                'order' => array('State.name' => 'asc'
            )));
        $this->set('salestaxes', $this->paginate());
        $states = $this->Salestax->State->find('list', array('fields' => array('State.id', 'State.name'), 'order' => array('State.name ASC')));
        $this->set(compact('states'));
    }

    function backstage_view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid salestax', true));
            $this->redirect(array('action' => 'index'));
        }

        $this->set('salestax', $this->Salestax->read(null, $id));
    }

    function backstage_add() {
        if (!empty($this->data)) {
            $this->data['Salestax']['seller_id'] = $this->Auth->user('id');
            $this->Salestax->create();
            if ($this->Salestax->save($this->data)) {
                $this->Session->setFlash(__('The sales tax has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The sales tax could not be saved. Please, try again.', true));
            }
        }
        $states = $this->Salestax->State->find('list');
        $this->set(compact('states'));
    }

    function edittax($id = null) {
        $this->layout = '';
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid salestax', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->data['Salestax']['seller_id'] == $this->Auth->user('id')) {
                if ($this->Salestax->save($this->data)) {
                    $this->Session->setFlash(__('The sales tax has been updated successfully.', true));                    
                    $this->redirect(array('action' => 'index',$this->data['Salestax']['product_id']));
                } else {
                    $this->Session->setFlash(__('The sales tax could not be saved. Please, try again.', true));
                }
            } else {
                $this->data['Salestax']['id'] = '';
                $this->data['Salestax']['seller_id'] = $this->Auth->user('id');
                if ($this->Salestax->save($this->data)) {
                    $this->Session->setFlash(__('The sales tax has been saved', true));
                    $this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash(__('The sales tax could not be saved. Please, try again.', true));
                }
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Salestax->read(null, $id);
        }
        $states = $this->Salestax->State->find('list', array('fields' => array('State.id', 'State.name'), 'order' => array('State.name ASC')));
        $this->set(compact('states'));
    }

    function edit($id = null) {
        $this->layout = '';
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid salestax', true));
            $this->redirect(array('action' => 'index',$this->data['Salestax']['product_id']));
        }
        if (!empty($this->data)) {
            if ($this->Salestax->save($this->data)) {
                //$this->Session->setFlash(__('The sales tax has been saved', true));
                $this->render('success');
                //$this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The sales tax could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Salestax->read(null, $id);
        }
        $states = $this->Salestax->State->find('list', array('fields' => array('State.id', 'State.name'), 'order' => array('State.name ASC')));
        $this->set(compact('states'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for salestax', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Salestax->delete($id)) {
            $this->Session->setFlash(__('Salestax deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Salestax was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

    function deletesales($id = null) {
        $this->layout = false;
        $return_arr['RowId'] = $id;
        if (!$id) {
            $return_arr['msg'] = 'Invalid id for salestax';
        }
        if ($this->Salestax->delete($id)) {
            $return_arr['msg'] = 'Salestax deleted';
        }
        echo json_encode($return_arr);
        $this->autoRender = false;
    }

    function backstage_delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for salestax', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Salestax->delete($id)) {
            $this->Session->setFlash(__('Salestax deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Salestax was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }
}
?>